#include "StdAfx.h"
#include "SituationEvaluationMode.h"


CSituationEvaluationMode::CSituationEvaluationMode(CString filePath): m_GoBoardWithCommonProcess(19),
	m_SGFFile(filePath),
	m_LivingPiecesBoard(19)
{
	m_MoveScript = 0;
	m_IsShuziStatus = false;
}


void CSituationEvaluationMode::Init(void)
{
	m_GoBoardWithCommonProcess.Init();
	m_SGFFile.Init();
	m_SGFFile.CopyColorOrder(&m_ColorOrder);
	m_SGFFile.CopyMoveOrder(&m_MoveOrder);
}


CSituationEvaluationMode::~CSituationEvaluationMode(void)
{
}


void CSituationEvaluationMode::Move(void)
{
	if (m_MoveScript >= m_MoveOrder.size()) return;
	m_GoBoardWithCommonProcess.m_PlayerToMove = m_ColorOrder[m_MoveScript];
	m_GoBoardWithCommonProcess.Move(m_MoveOrder[m_MoveScript]);
	m_MoveScript++;
}


void CSituationEvaluationMode::Back(void)
{
	unsigned lastMoveScript = m_MoveScript - 1;
	BackToBegin();
	while (m_MoveScript < lastMoveScript) {
		Move();
	}
}


void CSituationEvaluationMode::MoveToEnd(void)
{
	while (m_MoveScript < m_MoveOrder.size()) {
		Move();
	}
}


void CSituationEvaluationMode::BackToBegin(void)
{
	m_GoBoardWithCommonProcess.Clear();
	m_MoveScript = 0;
	PlaceRangzi();
}


void CSituationEvaluationMode::SynchronizeLivingPiecesBoard(void)
{
	m_LivingPiecesBoard.Copy(m_GoBoardWithCommonProcess.m_GoBoard);
}


void CSituationEvaluationMode::PlaceRangzi(void)
{
	TLocationVector rangziVector;
	m_SGFFile.CopyRangziVector(&rangziVector);
	for (TLocationVector::iterator item=rangziVector.begin(); item!=rangziVector.end(); ++item) {
		m_GoBoardWithCommonProcess.SetRangzi(*item);
		TRACE("(%d, %d)\n", item->x, item->y);
	}
}


void CSituationEvaluationMode::Shuzi(void)
{
	m_BlackArea.clear();
	m_WhiteArea.clear();
	SGameResult consequence;
	m_GoBoardWithCommonProcess.Shuzi(m_LivingPiecesBoard, &m_BlackArea, &m_WhiteArea, &consequence);
	m_BlackScore = consequence.blackScore;
	m_WhiteScore = consequence.whiteScore;
}